Run a Report with SQL Data in the Java Embedded Engine
This tutorial will guide you through running a document with SQL data in the Java Embedded Engine. It will go over installing the Java Embedded Engine, and setting up the code to run a report with SQL data.
Before installation of the Java Embedded Engine, make sure you meet the requirements and have the necessary pre-requisites installed. Those can be found here.
Install the Java Embedded Engine
There are two ways to install the Java Embedded Engine.
- Maven
- Manual Installation with zip file
Maven
To include the Java Engine in your project via Maven, follow the instructions found here.
Manual Installation with zip file
The other approach is referencing the Java Embedded Engine by downloading the zip file which contains the necessary JARs. These can be found on our downloads page here.
Once you have downloaded the zip file, extract the contents and reference the JARs in your project. The steps to do this can be found here.
Install JDBC Drivers
For SQL datasources, the Java Embedded Engine requires separate JDBC drivers to be installed depending on the connection type. Instructions on how to download and install the driver for each supported SQL datasource can be found here. This example uses the Microsoft SQL Server driver.
Setup the Properties
Once you have installed the Java Embedded Engine, you will need to set up the necessary properties in the properties file so you can generate documents. In your projects resources
directory, create a file called "WindwardReports.properties". This file will contain the properties related to document generation. The only property that is required to get started is the license. If this is empty you can still generate output, however it will contain a watermark.
To add your license key or any report properties, you need to add a WindwardReports section to your projects WindwardReports.properties file:
license=[[LICENSE]]
To learn more about the different properties we provide for report generation, check out this article.
Generating a Document
Now that you have the Java Embedded Engine installed and the properties set up, you can start generating documents. Below is an example of how to generate a document with SQL data.
import net.windward.datasource.DataSourceProvider;
import net.windward.datasource.jdbc.JdbcDataSource;
import net.windward.xmlreport.ProcessPdf;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class JavaSqlSample {
public static void main(String[] args) {
try {
// To generate a report, first we need a ProcessReport object. For now, we're using the
// pdf format to output.
File fileReport = new File("report.pdf");
FileInputStream template = new FileInputStream("SQL - Template.docx");
FileOutputStream reportStream = new FileOutputStream(fileReport);
ProcessPdf report = new ProcessPdf(template, reportStream);
// Preparation...
report.processSetup();
// Set up the datasource.
String connectionString = "jdbc:sqlserver://mssql.windward.net;DatabaseName=Northwind;User=demo;Password=demo"; //The Database connection string
String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //The database connection driver
DataSourceProvider datasource = new JdbcDataSource(driverClassName, connectionString);
// Finally, send it to Fluent Engine for processing. The second parameter is the name of the
// datasource. This should match the name used in your template.
report.processData(datasource, "SqlData");
// And... DONE!
report.processComplete();
template.close();
reportStream.close();
} catch (Exception e) {
// Uh oh, just in case
e.printStackTrace();
}
}
}